package com.example.view;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.logging.Handler;

public class SearchView extends View {
    //画笔
    private Paint mPaint;
    private int mViewWidth;
    private int mViewHeight;

    //这个视图拥有的状态
    private static enum State {
        NONE,
        STARTING,
        SEARCHING,
        ENDING
    }

    //当前的状态(非常重要)
    private State mCurrentState = State.NONE;

    //放大镜与外部圆环
    private Path path_srarch;
    private Path path_circle;
    //测量Path并截取部分的工具
    private PathMeasure mMeasure;
    //默认的动效周期2s
    private int defaultDuration = 2000;

    //控制各个过程的动画
    private ValueAnimator mStartingAnimator;
    private ValueAnimator mSearchingAnimator;
    private ValueAnimator mEndingAnimator;

    //动画数值(用于控制动画状态，因为同一时间内只允许有一种状态出现，具体数值处理取决于当前状态)
    private float mAnimatorValue = 0;

    //动效过程监听器
    private ValueAnimator.AnimatorUpdateListener mUpdateListener;
    private Animator.AnimatorListener mAnimatorListener;

    //用于控制动画状态转换
    private Handler mAnimatorHandler;
    //判断是否搜索已经结束
    private boolean isOver = false;
    private int count = 0;

    public SearchView(Context context) {
        super(context, null);
    }

    public SearchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAll();
    }

    private void initAll() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(15);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setAntiAlias(true);
    }
}
